package com.wangsh.userscenter.util;

import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;

import com.wangsh.userscenter.common.util.ConstatFinalUtil;
import com.wangsh.userscenter.common.util.SpringEmailUtil;

/**
 * 多线程
 * 
 * @author wangsh_sxt
 *
 */
public class ServiceCallableUtil implements Callable<Integer>
{
	/* 操作类型 */
	private String operType;
	/* 存储参数的map */
	private Map<String, Object> paramsMap;

	@Override
	public Integer call() throws Exception
	{
		try
		{
			String thid = UUID.randomUUID().toString().replaceAll("-", "");
			Date createTime = new Date();
			paramsMap.put("createTime", createTime);
			paramsMap.put("thid", thid);
			//1是开始,0是停止
			paramsMap.put("status", "1");
			ConstatFinalUtil.THREAD_MAP.put(thid, paramsMap);
			
			ConstatFinalUtil.SYS_LOG.info(thid + "-----线程池开始-----");
			if("sendEmail".equalsIgnoreCase(operType))
			{
				//发线程发邮件
				sendEmail();
			}
			//将线程移除掉
			ConstatFinalUtil.THREAD_MAP.remove(thid);
			ConstatFinalUtil.SYS_LOG.info(thid + "-----线程池结束-----");
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOG.error("启动多线程报错了;条件:{}", paramsMap + ";operType:" + this.operType,e);
		}
		return 0;
	}
	
	/**
	 * 采用多线程发邮件
	 */
	private void sendEmail()
	{
		SpringEmailUtil springEmailUtil = (SpringEmailUtil) paramsMap.get("springEmailUtil");
		String email = paramsMap.get("email") + "";
		String subject = paramsMap.get("subject") + "" ; 
		String content = paramsMap.get("content") + "" ; 
		springEmailUtil.sendHTMLMail(email, subject, content);
		ConstatFinalUtil.SYS_LOG.info(email + "-----发送邮件线程-----" + subject);
	}

	public void setOperType(String operType)
	{
		this.operType = operType;
	}

	public void setParamsMap(Map<String, Object> paramsMap)
	{
		this.paramsMap = paramsMap;
	}
}
